package mobi.greenplate.client.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mobi.greenplate.client.domain.User;
import mobi.greenplate.client.domain.UserDetail;
import mobi.greenplate.client.converter.UserDetailConverter;
import mobi.greenplate.client.converter.UserConverter;
import mobi.greenplate.client.parser.DomainHandler;

/**
 *
 * @author dmakariev
 */
public class UserDetailListHandler extends DomainHandler<List<UserDetail>> {

    private static final String USER_DETAIL_TAG = "userDetail";
    private static final String USER_DETAIL_ROOT = "//userDetail-list/userDetail";
    private static final String USER_DETAIL_OWNER_ROOT = "//userDetail-list/userDetail/owner";
    private static final String OWNER_TAG = "owner";
    private final List<UserDetail> userDetailList = new ArrayList<UserDetail>();
    private UserDetail currentUserDetail;
    private User currentOwner;

    @Override
    public List<UserDetail> getEntity() {
        return Collections.unmodifiableList(userDetailList);
    }

    @Override
    public void endTag(String parentPath, String tagName, String text) {
        if (parentPath.equalsIgnoreCase(USER_DETAIL_ROOT)) {
            currentUserDetail = UserDetailConverter.endTag(currentUserDetail, tagName, text);
        }
        if (parentPath.equalsIgnoreCase(USER_DETAIL_OWNER_ROOT)) {
            currentOwner = UserConverter.endTag(currentOwner, tagName, text);
        }
    }

    @Override
    public void startTag(String parentPath, String tagName) {
        if (tagName.equalsIgnoreCase(USER_DETAIL_TAG)) {
            currentUserDetail = new UserDetail();
            userDetailList.add(currentUserDetail);
        }
        if (tagName.equalsIgnoreCase(OWNER_TAG)) {
            currentOwner = new User();
            currentUserDetail.setOwner(currentOwner);
        }
    }
}
